package com.fwtai.service.api;

import com.fwtai.api.ApiGoodsDao;
import com.fwtai.bean.PageFormData;
import com.fwtai.config.LocalUserId;
import com.fwtai.service.AsyncService;
import com.fwtai.tool.ToolClient;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * 门店管理业务层
 * @作者 田应平
 * @版本 v1.0
 * @QQ号码 444141300
 * @创建日期 2023-04-04 17:45:50
 * @官网 <url>http://www.fwtai.com</url>
*/
@Service
public class ApiGoodsService{

  private final ApiGoodsDao apiGoodsDao;

  private final AsyncService asyncService;

  private final ThreadPoolTaskExecutor taskExecutor;

  public ApiGoodsService(final ApiGoodsDao apiGoodsDao,final AsyncService asyncService,final ThreadPoolTaskExecutor taskExecutor){
    this.apiGoodsDao = apiGoodsDao;
    this.asyncService = asyncService;
    this.taskExecutor = taskExecutor;
  }

  public String queryById(final Long kid,final Integer type){
    return CompletableFuture.supplyAsync(()->{
      if(kid == null || type == null){
        return ToolClient.jsonValidateField();
      }
      switch (type){
        case 1://1=直营门店的商品详情;
          return ToolClient.queryJson(apiGoodsDao.queryGoodsType1(kid));
        case 2://2=区级代理的商品详情;
          return ToolClient.queryJson(apiGoodsDao.queryGoodsType2(kid));
        case 3://3=总公司的商品详情
          return ToolClient.queryJson(apiGoodsDao.queryGoodsType3(kid));
        default:
          return ToolClient.jsonValidateField();
      }
    },taskExecutor).join();
  }

  /**适用于api接口,仅含数据不含总记录数总条数*/
  public String listData(final HttpServletRequest request){
    return CompletableFuture.supplyAsync(()->{
      final PageFormData formData = ToolClient.pageParamsApi(request);
      if(formData == null) return ToolClient.jsonValidateField();
      final List<HashMap<String,Object>> list = apiGoodsDao.getListData(formData);
      return ToolClient.jsonPage(list);
    },taskExecutor).join();
  }

  //获取获取区级[合伙人]的商品列表
  public String listPagePartner(final HttpServletRequest request){
    return CompletableFuture.supplyAsync(()->{
      final PageFormData formData = ToolClient.pageParamsApi(request);
      if(formData == null) return ToolClient.jsonValidateField();
      return ToolClient.jsonPage(apiGoodsDao.listPagePartner(formData));
    },taskExecutor).join();
  }

  public String addPriorityProxy(final String kid,final String goodsId){
    final Long loginId = LocalUserId.getLong();
    return CompletableFuture.supplyAsync(()->{
      if(kid == null || goodsId == null){
        return ToolClient.jsonValidateField();
      }
      if(loginId == null){
        return ToolClient.accessLogin();
      }
      final HashMap<String,Long> params = new HashMap<>(2);
      params.put("goodsId",Long.parseLong(goodsId));
      params.put("loginId",loginId);
      return ToolClient.executeRows(apiGoodsDao.addPriorityProxy(Long.parseLong(kid),params));
    },taskExecutor).join();
  }

  public String addPriorityStore(final String kid,final String goodsId,final String storeId){
    final Long loginId = LocalUserId.getLong();
    return CompletableFuture.supplyAsync(()->{
      if(loginId == null){
        return ToolClient.accessLogin();
      }
      if(kid == null || storeId == null || goodsId == null){
        return ToolClient.jsonValidateField();
      }
      final HashMap<String,Long> params = new HashMap<>(2);
      params.put("goodsId",Long.parseLong(goodsId));
      params.put("storeId",Long.parseLong(storeId));
      return ToolClient.executeRows(apiGoodsDao.addPriorityStore(Long.parseLong(kid),params));
    },taskExecutor).join();
  }

  public String listInventoryProxy(final HttpServletRequest request){
    final Long loginId = LocalUserId.getLong();
    return CompletableFuture.supplyAsync(()->{
      if(loginId == null) return ToolClient.accessLogin();
      final PageFormData formData = ToolClient.pageParamsApi(request);
      if(formData == null) return ToolClient.jsonValidateField();
      formData.put("loginId",loginId);
      return ToolClient.jsonPage(apiGoodsDao.listInventoryProxy(formData));
    },taskExecutor).join();
  }

  public String listInventoryStore(final HttpServletRequest request){
    final Long loginId = LocalUserId.getLong();
    return CompletableFuture.supplyAsync(()->{
      if(loginId == null) return ToolClient.accessLogin();
      final PageFormData formData = ToolClient.pageParamsApi(request);
      if(formData == null) return ToolClient.jsonValidateField();
      final Long storeId = apiGoodsDao.getStoreUser(loginId);
      if(storeId == null){
        return ToolClient.jsonValidateField("抱歉,您不是店主");
      }
      formData.put("storeId",storeId);
      return ToolClient.jsonPage(apiGoodsDao.listInventoryStore(formData));
    },taskExecutor).join();
  }

  public String listCouponProxy(final HttpServletRequest request){
    final Long loginId = LocalUserId.getLong();
    return CompletableFuture.supplyAsync(()->{
      if(loginId == null) return ToolClient.accessLogin();
      final PageFormData formData = new PageFormData(request);
      final Long goodsId = formData.getLong("goodsId");
      if(goodsId == null) return ToolClient.jsonValidateField();
      formData.put("loginId",loginId);
      formData.put("goodsId",goodsId);
      return ToolClient.jsonPage(apiGoodsDao.listCouponProxy(formData));
    },taskExecutor).join();
  }

  public String listCouponStore(final HttpServletRequest request){
    final Long loginId = LocalUserId.getLong();
    return CompletableFuture.supplyAsync(()->{
      if(loginId == null) return ToolClient.accessLogin();
      final PageFormData formData = new PageFormData(request);
      final Long storeId = formData.getLong("storeId");
      final Long goodsId = formData.getLong("goodsId");
      if(storeId == null || goodsId == null) return ToolClient.jsonValidateField();
      formData.put("storeId",storeId);
      formData.put("goodsId",goodsId);
      return ToolClient.jsonPage(apiGoodsDao.listCouponStore(formData));
    },taskExecutor).join();
  }

  public String getGoods(final String code){
    final Long loginId = LocalUserId.getLong();
    return CompletableFuture.supplyAsync(()->{
      if(loginId == null) return ToolClient.accessLogin();
      if(code == null){
        return ToolClient.jsonValidateField("无效的防伪码");
      }
      final HashMap<String,Object> map = apiGoodsDao.getGoods(code);
      if(map != null){
        if(map.get("check_time") == null){
          final HashMap<String,Object> params = new HashMap<>(2);
          params.put("user_id",loginId);
          params.put("code",code);
          //首次查看时间
          asyncService.editCheckTime(params,taskExecutor);
        }
        asyncService.updateCheckTotal(code,taskExecutor);
      }
      return ToolClient.queryJson(map);
    },taskExecutor).join();
  }
}